package com.golf.client.player;

import java.awt.event.MouseEvent;
import java.util.concurrent.atomic.AtomicBoolean;

import com.golf.client.ui.MouseEventsAdaptor;
import com.golf.core.course.bodies.Ball;
import com.golf.core.course.bodies.Position;
import com.golf.core.course.bodies.Vector;
import com.golf.core.math.util.VectorUtil;
import com.golf.core.player.Player;
import com.golf.server.engine.phys.Engine;


/**
 * 
 * 
 * @author gfloodgate
 */
public class InteractivePlayer extends MouseEventsAdaptor implements Player
{

    /** The players name. */
    private final String name;

    
    /** Is the player currently interacting with the system? */
	private AtomicBoolean isInteracting = new AtomicBoolean();

	/** Is it currently this players turn. */
    private AtomicBoolean isTurn = new AtomicBoolean();;
	
	/** The ball that this player is using. */
	private Ball ball;
	
	
	/** The vector the player's aim. */
	private Vector aimVector = new Vector(0, 0);

	
	
	
	private Engine engine;

	
	
	/**
	 * Create an instance of interactive player.
	 * 
	 * @param name The name of this player.
	 * @param engine The world engine that this player is interacting with.
	 * @param ball The ball that is going to be hit.
	 */
	public InteractivePlayer(final String name, final Engine engine, final Ball ball)
	{
	    this.name = name;
		this.engine = engine;
		this.ball = ball;
	}
	
	
	public void setBall(final Ball ball)
	{
	    this.ball = ball;
	}
	
	
	/**
	 * Get the vector the player has used.
	 * 
	 * @return The aim vector for the player.
	 */
	public Vector getAimVector()
	{
		return aimVector;
	}
	
	
	/**
	 *
	 * 
	 * @return
	 */
	public boolean isInteracting()
	{
		return isInteracting.get();
	}
	
	
   /**
    *
    * 
    * @return
    */
   public boolean isTurn()
   {
       return isTurn.get();
   }


	/**
	 * Get the ball that this player is using.
	 * 
	 * @return The ball that the player is using.
	 */
	public Ball getBall() 
	{
		return ball;
	}
	
	
	/**
	 * {@inheritDoc}
	 */
	public void mouseMoved(final MouseEvent e) 
	{
		double x = (double)e.getX();
		double y = (double)e.getY();
		
		final Position ballPosition = ball.getPosition();
		
		double power = Math.abs(Math.sqrt( Math.pow((ballPosition.getX() - x),2) + Math.pow((ballPosition.getY() -y),2) ));
		double angle = VectorUtil.angleBetweenTwoVectors(0, 10, ballPosition.getX() - x, ballPosition.getY() - y);
			
		aimVector = new Vector((float)angle, (float)power);
	}

	
	/**
	 * {@inheritDoc}
	 */
	public void mouseClicked(final MouseEvent e) 
	{
	    if (isTurn())
	    {
    		if (e.getButton() == MouseEvent.BUTTON1)
    		{
    	        System.out.println("Playing shot with angle: " + aimVector.toString());
    			
    	        // NB: right now the renderer is rendering THIS engine -> fix it to remove this !!!!! should rewnder game engine
    	        // GAME engine needs to send update messages  ...
    	        
    	        engine.playShot(ball, aimVector);
    			
                synchronized (aimVector)
                {
                    aimVector.notifyAll();
                }
                
    			// Left Click.
    			//System.out.println("Left mouse button click.");
    		}
    		else if (e.getButton() == MouseEvent.BUTTON2)
    		{
    			// Right Click.
    			//System.out.println("Right mouse button click.");
    		}
	    }
	}

	
	/**
	 * {@inheritDoc}
	 */
	public void mouseEntered(final MouseEvent e) 
	{
		isInteracting.set(true);
		System.out.println("Entered canvas.");
	}

	
	/**
	 * {@inheritDoc}
	 */
	public void mouseExited(final MouseEvent e) 
	{
		isInteracting.set(false);
		System.out.println("Exited canvas.");
	}


    /**
     * {@inheritDoc}
     */
    public String name()
    {
        return name;
    }
    

    /**
     * {@inheritDoc}
     */
    public Vector chooseShot()
    {
        isTurn.set(true);

        try
        {
            //Thread.sleep(5000); 
            synchronized (aimVector)
            {
                aimVector.wait();
            }
        } 
        catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        isTurn.set(false);
        return aimVector;
    }

}
